﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ETL.Business.Models;
using ETL.Common.Logging;
using ETL.DataAccess;
using ETL.Entity;

namespace ETL.Business.Handlers.ProjectUserHandler
{

    public class GetUserOfTask
    {
        private readonly ProjectUserRepository _projectUserRepository;

        public GetUserOfTask()
        {
            _projectUserRepository = new ProjectUserRepository();
        }
        public GetUserOfTask(ProjectUserRepository projectUserRepository)
        {
            _projectUserRepository = projectUserRepository;
        }

        public virtual List<ProjectUserModel> Execute(string taskId)
        {
            IEnumerable<ProjectUserModel> users = null;

            try
            {
                decimal _taskId = 0;
                decimal.TryParse(taskId, out _taskId);

                // try to get from db
                var appUsers = _projectUserRepository.GetAppUserOfTask(_taskId);

                // parse to business model
                if (appUsers != null)
                {
                    users = from a in appUsers select new ProjectUserModel(a);
                }

                if(users!= null && users.Count() > 0)
                    return users.ToList();
                else
                    return new List<ProjectUserModel>();
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
                return null;
            }
        }
    }
}
